home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / share / Dos / VARIOS / pascal / SWAG9605.DDD / 0118_LTRIM & assembler.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-05-31  |  1.8 KB  |  59 lines

  1. (*
  2. In a message dated Wednesday March 13 1996, Mario Polycarpou of 3:690/354
  3. wrote:
  4.  DR>> function LTrim(S: string): string;
  5.  DR>> var C: Byte;
  6.  DR>> begin
  7.  DR>> for C := 1 to Length(S) do if S[C+1]<>#32 then Break;
  8.  DR>> LTrim := Copy(S,C,255);
  9.  DR>> end;
  10.  
  11.  MP>  Sorry mate but that's crap. Have a play with this...
  12.  
  13.  MP> {-+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--}
  14.  MP> FUNCTION TrimL(S:String):String;     {Trim left}
  15.  MP> VAR X:Integer;
  16.  MP> BEGIN
  17.  MP>  X:=1;
  18.  MP>  WHILE S[X]=#32 DO Inc(X);
  19.  MP>  TrimL:=Copy(S,X,Length(S));
  20.  MP> END;
  21.  MP> {-+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--}
  22.  
  23. OK, by my calculations, TrimL is about 10% faster - try this, it's about twice
  24. as fast as Marios:
  25. *)
  26.  
  27. function StripLBStr (const S: string): string; assembler;
  28.  
  29. asm
  30.       mov  dx, ds     { Save DS Register }
  31.       cld
  32.       les  di, [S]    { ES:DI => Source }
  33.       mov  al, es:[DI]{ Load Length of String }
  34.       inc  di
  35.       sub  cx, cx     { Set CX to Zero }
  36.       mov  cl, al     { CX <- No. of Bytes }
  37.       jcxz @1         { if Null String then Terminate }
  38.       mov  ax, ' '    { Store ' ' in AX }
  39.       repz scasb      { Scan String until no Space or string scan complete }
  40.       jz   @3
  41.       inc  cx
  42.       dec  di
  43.       push es
  44.       pop  ds
  45.       mov  si, di
  46.   @3: mov  al, cl
  47.   @1: les  di, @Result{ ES:DI => Destination }
  48.       stosb           { store length byte }
  49.       jcxz @2         { if CX = 0 then done }
  50.       movsb           { Move first char so stay word aligned }
  51.       dec  cx
  52.       jcxz @2
  53.       shr  cx,1       { CX <- CX div 2 }
  54.       rep  movsw      { move rest as words }
  55.       jnc  @2         { if carry then odd number }
  56.       movsb           { so move the odd one }
  57.   @2: mov  ds, dx     { Restore DS Register }
  58. end;
  59.